import {getUrlParam} from '@/static/js/filter.js'
import member from '@/components/selectMember/selectMember.vue'
export default {
	components:{member},
	data() {
		return {
			userName:'',
			userId: '',
			dialog: false,
			specifier: '',
			advice: '',
			catalog:[],
			subjective: [],
			objective: [],
			theInput: '',
			checkArr:[
				{title:'问题目录',check:'重要的体检阳性结果，重要的不良生活方式行为。',name:'catalog',data:[]},
				{title:'主观描述',check:'主诉、症状、患者的主观感觉、疾病史、家族史和社会生活史等。',name:'subjective',data:[]},
				{title:'客观资料',check:'包括体检所见、实验室检查结果、心理行为测量结果以及医生观察到的患者的态度、行为。',name:'objective',data:[]},
				{title:'不可改变的危险因素',name:'notRiskFactors',data:[]},
				{title:'重点健康危险因素提示（可改变）',name:'healthRiskFactors',data:[]},
				{title:'可改善危险因素空间',name:'riskFactorSpace',data:[]},
				{title:'其他问题',name:'otherIssues',data:[]},
			],
			showModal: false,
			showContent: '',
			id: '',
			editIndex:'',
			editData:{}
		}
	},
	onLoad() {
		const _this = this;
		_this.id = getUrlParam('id');
		if(_this.id) {
			_this.$api.HealthRisksInfo(_this.id).then((res) => {
				if(res.data.returnCode == '200') {
					_this.userId = res.data.result && res.data.result.userId;
					_this.userName = res.data.result && res.data.result.userName;
					_this.checkArr.map((a) => {
						if(a.name == "catalog") {
							a.data = res.data.result.catalog && JSON.parse(res.data.result.catalog).map((b) => {
								return {'catalog':b.value ||''}
							})
						} else if(a.name == 'subjective') {
							a.data = res.data.result.subjective && JSON.parse(res.data.result.subjective).map((b) => {
								return {'subjective':b.value ||''}
							})
						}else if(a.name == 'objective') {
							a.data = res.data.result.objective && JSON.parse(res.data.result.objective).map((b) => {
								return {'objective':b.value ||''}
							})
						}else if(a.name == 'notRiskFactors') {
							a.data = res.data.result.notRiskFactors && JSON.parse(res.data.result.notRiskFactors).map((b) => {
								return {'notRiskFactors':b.value ||''}
							})
						}else if(a.name == 'healthRiskFactors') {
							a.data = res.data.result.healthRiskFactors && JSON.parse(res.data.result.healthRiskFactors).map((b,bi) => {
								const DOM = '#healthRiskFactors'+bi
								setTimeout(()=>{uni.createSelectorQuery().select(DOM).context((c) => {
									this.editorCtx = c.context; this.editorCtx.setContents({html: b.value || ''})}).exec()},200)
								return {'healthRiskFactors':b.parameterValue.name,value:b.parameterValue.value}
							})
						}else if(a.name == 'riskFactorSpace') {
							a.data = res.data.result.riskFactorSpace && JSON.parse(res.data.result.riskFactorSpace).map((b) => {
								return {'riskFactorSpace':b.value ||''}
							})
						}else if(a.name == 'otherIssues') {
							a.data = res.data.result.otherIssues && JSON.parse(res.data.result.otherIssues).map((b) => {
								return {'otherIssues':b.value ||''}
							})
						}
						return a
					})
					// console.log(_this.checkArr)
				}
			})
		}
	},
	methods: {
		hidedialog() {
			this.dialog = false
		},
		onEditorReady() {
			this.checkArr.map((a,ai)=>{
				if(a.name == 'healthRiskFactors') {
					a.data.map((b,bi)=>{
						const DOM = '#'+a.name+bi
						setTimeout(()=>{
							uni.createSelectorQuery().select(DOM).context((c) => {
								if(c && c.context){
									this.editorCtx = c.context;
									this.editorCtx.setContents({html: b.value})
								}
							}).exec()},200)
					})
				}
				
			})

		},
		chooseUser(e){ //选择会员
			this.userName = e.userName;
			this.userId = e.userId;
		},
		add(e) {
			this.checkArr[e].data.push({});
		},
		del(e,index) {
			this.checkArr[e].data.splice(index,1)
		},
		editorInput2(e){
			this.editData[this.editIndex].value = e.value.detail.html;
			// console.log(this.checkArr)
		},
		submit(e){
			if(!this.userId) {
				uni.showToast({
					title: '请先选择会员',
					icon: 'none'
				})
				return
			}
			this.AppHealthRisksEntityReq = {
				userId: this.userId ? this.userId : '',
				status: e,
				healthId: localStorage.userInfo ? JSON.parse(localStorage.userInfo).id : ''
			}
			this.checkArr.map((a)=>{
				if(a.name == 'healthRiskFactors') {
					this.AppHealthRisksEntityReq[a.name]= a.data.map((b,bindex) => {
						// console.log(b.value)
						return {
							name: bindex,
							parameterValue:{
								name: b[a.name],
								value: b.value
							}
						}
					})
					
				}else {
					this.AppHealthRisksEntityReq[a.name] = a.data.map((b,bindex) => {
						return {
							name: a.name,
							value: b[a.name]
						}								
					}) 
				}
			})
			if(this.id) {
				this.AppHealthRisksEntityReq.id = Number(this.id);
				if(e == 1) {
					this.$api.HealthRisksUpdata(this.AppHealthRisksEntityReq).then((res) => {
						if(res.data.returnCode == '200') {
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/quickly/summary/list'
								})
							},1000)
						} else if(res.data.returnCode == '506') {
							uni.showToast({
								title:res.data.message,
								icon: 'none'
							})
							
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/quickly/summary/list'
								})
							},1000)
						}
					})
					
				}else {
					this.showModal = true,
					this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改'
				}
			}else {
				if(e == 1) {
					this.$api.HealthRisksSave(this.AppHealthRisksEntityReq).then((res) => {
						if(res.data.returnCode == '200') {
							uni.showToast({
								title: '保存成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/quickly/summary/list'
								})
							},1000)
						}else if(res.data.returnCode == '506') {
							uni.showToast({
								title:res.data.message,
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/quickly/summary/list'
								})
							},1000)
						}
					})
				}
				else {
					this.showModal = true,
					this.showContent = '确认信息无误，签名并发送给用户存入健康档案，发送后该信息将不可修改'			
				}
			}
			
		},
		// 签名发送
		sureconfirm() {
			const url = this.id ? 'HealthRisksUpdata' : 'HealthRisksSave';
			this.$api[url](this.AppHealthRisksEntityReq).then((res) => {
				if(res.data.returnCode == '200') {
					uni.showToast({
						title: '签发成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/quickly/summary/list'
						})
					},1000)
				}
			})
		}
	}
}